package org.kwin.ibatis.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.kwin.spring.aop.aspectj.proxy.ProxyChain;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author : wangkai
 * @version V1.0
 * @Package org.kwin.ibatis.proxy
 * @Description:
 * @date 2021年10月09日 13:37
 **/
public class MapperProxyFactory {
    public static <T> T getProxy(Class clazz) {
//        InvocationHandler invocationHandler = new MapperInvocationHandler();
//        T t = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, invocationHandler);
//        return t;
    
        return (T) Enhancer.create(clazz, new MethodInterceptor() {
            /**
             * 代理方法, 每次调用目标方法时都会先创建一个 ProxyChain 对象, 然后调用该对象的 doProxyChain() 方法.
             */
            @Override
            public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable {
                return AnalyticMethod.invoke(clazz, targetObject, targetMethod, methodParams);
            }
        });
    }
}
